<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ include file="/WEB-INF/views/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<style type="text/css">
	</style>
  <script type="text/javascript">
	var datagrid;
	var rowEditor=undefined;
	var buttonFlag = null;
	$(function(){
		datagrid=$("#dg").datagrid({
			url:"${cs}/cs/credit/loanbid/list",//加载的URL
		    isField:"id",
			pagination:true,//显示分页
			pageSize:10,//分页大小
			pageList:[10,15,20],//每页的个数
			fit:true,//自动补全
			fitColumns:true,
			iconCls:"icon-save",//图标
			columns:[[      //每个列具体内容
		              {field:'id', hidden:true },   
		              {field:'title', title:'标名',width:"10%",align:'center' },
		              {field:'content', title:'内容',width:"15%",align:'center' },
		              {field:'publisherName', title:'发布者账户',width:"10%",align:'center' },
		              {field:'securityName', title:'担保账户',width:"10%",align:'center' },
		              {field:'money', title:'借款金额',width:"10%",align:'center' ,
		            	  formatter: function(val,row, index) {
			            		return "￥"+val;
			            	} 
		              },
		              {field:'months', title:'贷款期限',width:"10%",align:'center',
		            	  formatter: function(val,row, index) {
			            		return val+"个月";
			            	}   
		              },
		              {field:'credittime', title:'资金到账时间',  width:"15%", align:'center',
		            	  formatter:function(value,row,rowIndex) { 
		            		  if(row.state>3&& row.state!=9){
		            		  var date = new Date(value);//long转换成date
		                      var month = (date.getMonth() + 1);
		                      var day = date.getDate().toString();
		                      if (month < 10) {
		                          month = "0" + month;
		                      }
		                      if (day < 10) {
		                          day = "0" + day;
		                      }
		                      return date.getFullYear().toString() + "年" + month + "月" + day+"日"+date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒";
		            		 } 
		            	} 
			         },
		             {field:'state', title:'审核状态',  width:"11%", align:'center',
			            	formatter: function(val,row, index) {
			            		switch(row.state){
			            			case 1:return '<span style="color:#FF44AA">待担保人确认</span>';
			            			case 2:return '<span style="color:#FFCC22">待系统审核</span>';
			            			case 3:return '<span style="color:#A044B0">已发布</span>';
			            			case 4:
			            				if(new Date(row.repaymentTime)<new Date()){
			            					return '<span style="color:#33FF33">已逾期，可垫付</span>';
			            				}else{
			            					return '<span style="color:#0000FF">同意贷款</span>'
			            				}
			            			case 5:return '<span style="color:#4B0082">逾期垫付</span>';
			            			case 6:return '<span style="color:#000000">已还款</span>';
			            			case 9:return '<span style="color:red">审核未通过</span>';
			            			default: return;
			            		}
			            	}  
			              },
			         {field:'crediterName', title:'放款账户',width:"10%",align:'center' },
		          ]],
			toolbar:[              //工具条
			        {text:"审核通过",iconCls:"icon-filter",handler:function(){
			        	var rows=datagrid.datagrid('getSelections');
			        	if(rows.length<=0){
			        		$.messager.alert('警告','您没有选择','error');
			        	}else{
			        		var rows = datagrid.datagrid('getSelections');
			        		for(var i=0; i<rows.length; i++){
	        					if(rows[i].state != 2){
	        						$.messager.alert('错误', "您只能操作待系统审核的贷款标数据！", 'error');
				        			return;
	        					}
	        				}
			        		$.messager.confirm('确定','您确定要审核通过，并发布此贷款标吗？',function(t){
			        			if(t){
			        				var ids = [];
			        				var rows = datagrid.datagrid('getSelections');
			        				for(var i=0; i<rows.length; i++){
			        					ids.push(rows[i].id);
			        				}
			        				$.ajax({
			        					url : '${cs}/cs/credit/loanbid/updateStatus',
			        					data : 'status=3&ids='+ids.join(','),
			        					method: 'POST',
			        					type:'json',
			        					dataType : 'json',
			        					success : function(r) {
			        						if (r>0) {
			        							$.messager.show({
			        								msg : "操作成功",
			        								title : '成功'
			        							});
			        							datagrid.datagrid('reload');
			        						} else {
			        							$.messager.alert('错误',"操作失败", 'error');
			        						}
			        						datagrid.datagrid('unselectAll');
			        					}
			        				});
			        			
			        			}
			        		})
			        	}
			        }
					},{text:"逾期垫付",iconCls:"icon-remove",handler:function(){
						var rows=datagrid.datagrid('getSelections');
			        	if(rows.length==1){
			        		if(rows[0].state==4 &&new Date(rows[0].repaymentTime)<new Date()){
			        			$.post("${cs}/cs/credit/loanbid/overdue?id="+rows[0].id,function(r){
			        				if(r.state==1)datagrid.datagrid('reload');
			        				$.messager.show({
			        					title:r.state==1?"Success":"Error",
			        					msg:r.msg
			        				})
			        			},"json")
			        		}else{
			        			$.messager.alert('错误', "您只能操作已逾期未还款的账户进行逾期还款！", 'error');
			        		}
			        	} else {
			        		$.messager.alert('错误', "只能选择一个数据进行修改", 'error');
			        	}
			        	}
					}
			  ]
		});
		$("#btn_search").click(function(){
			var param = $("#searchForm").serializeJson();
			datagrid.datagrid('load', param);
		});
		$('#state').combobox({
			 data: [
			        {"name": '全部', "id": '-1'},
			        {"name": '未通过', "id": '9'},
			        {"name": '待担保人确认', "id": '1'},
			        {"name": '待系统审核', "id": '2'},
			        {"name": '审核通过，已发布', "id": '3'},
			        {"name": '同意贷款', "id": '4'},
			        {"name": '逾期垫付', "id": '5'},
			        {"name": '已还款', "id": '6'}
			        ],
			 valueField:'id',
			 textField:'name',
			 value:'-1'
		 });
	});
	
    </script>
</head>
<body style="padding:0 4px; margin:0;  overflow: hidden; ">
<div class="easyui-layout" style="width:100%;height:100%;" data-options="fit:true">
		<div title="信贷管理" data-options="region:'north'" style="height:70px">
		 <form id="searchForm" >
			<table cellpadding="5">
				    		<tr>
				    			<td>贷款标名:</td>
				    			<td><input class="easyui-textbox" type="text"   name="title" ></input></td>
				    			<td>内容关键字:</td>
				    			<td><input class="easyui-textbox" type="text"   name="content" ></input></td>
				    			<td>审核状态:</td>
				    			<td><input class="easyui-combobox" type="text" data-options="panelHeight:'auto'" id="state"  name="state" ></input></td>
				    			<td ><a href="#" id="btn_search" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px">Search</a></td>
				    		</tr>
				    	</table>
				  </form>
		</div>
		<div data-options="region:'center'" >
			<table id="dg" >
			</table>
		</div>
		</div>
</body>
</html>